/**
 * Copyright (c) 2015-2016, Jadranka (4326518@qq.com).
 *
 */
package ${gen.packageName}.${gen.subPackageName}.model.base;

import java.math.BigInteger;

import com.jfinal.plugin.activerecord.IBean;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.msteam.app.sys.model.Metadata;
import com.msteam.app.sys.model.core.JModel;
import com.msteam.app.sys.model.query.MetaDataQuery;
import com.msteam.kernel.toolkit.listener.message.ListenerMessageKit;

/**
 * ${entity.DISPLAY_NAME} 基类
 * Auto generated by JadStudio, do not modify this file.
 * @author Administrator
 *
 * @param <M>
 */ 
@SuppressWarnings("serial")
public abstract class Base${entity.MODEL_NAME}<M extends Base${entity.MODEL_NAME}<M>> extends JModel<M> implements IBean {

	public static final String CACHE_NAME = "${entity.TABLE_NAME}";
	public static final String METADATA_TYPE = "${entity.TABLE_NAME}";

	public static final String ACTION_ADD = "${entity.TABLE_NAME}:add";
	public static final String ACTION_DELETE = "${entity.TABLE_NAME}:delete";
	public static final String ACTION_UPDATE = "${entity.TABLE_NAME}:update";

	public void removeCache(Object key){
		if(key == null) return;
		CacheKit.remove(CACHE_NAME, key);
	}

	public void putCache(Object key,Object value){
		CacheKit.put(CACHE_NAME, key, value);
	}

	public M getCache(Object key){
		return CacheKit.get(CACHE_NAME, key);
	}

	public M getCache(Object key,IDataLoader dataloader){
		return CacheKit.get(CACHE_NAME, key, dataloader);
	}


	<#list entity.fields as field>
	<#if field.IS_PRIMARY_KEY>
	public Metadata createMetadata(){
		Metadata md = new Metadata();
		md.setObjectId(BigInteger.valueOf(get${field.ATTR_NAME?cap_first}()));
		md.setObjectType(METADATA_TYPE);
		return md;
	}

	public Metadata createMetadata(String key,String value){
		Metadata md = new Metadata();
		md.setObjectId(BigInteger.valueOf(get${field.ATTR_NAME?cap_first}()));
		md.setObjectType(METADATA_TYPE);
		md.setMetaKey(key);
		md.setMetaValue(value);
		return md;
	}

	public boolean saveOrUpdateMetadta(String key,String value){
		Metadata metadata = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, BigInteger.valueOf(get${field.ATTR_NAME?cap_first}()), key);
		if (metadata == null) {
			metadata = createMetadata(key, value);
			return metadata.save();
		}
		metadata.setMetaValue(value);
		return metadata.update();
	}

	public String metadata(String key) {
		Metadata m = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, BigInteger.valueOf(get${field.ATTR_NAME?cap_first}()), key);
		if (m != null) {
			return m.getMetaValue();
		}
		return null;
	}
	</#if>	
	</#list>


	@Override
	public boolean save() {
		boolean saved = super.save();
		if (saved) { ListenerMessageKit.sendMessage(ACTION_ADD, this); }
		return saved;
	}

	@Override
	public boolean delete() {
		boolean deleted = super.delete();
		if (deleted) { ListenerMessageKit.sendMessage(ACTION_DELETE, this); }
		return deleted;
	}

	@Override
	public boolean deleteById(Object idValue) {
		boolean deleted = super.deleteById(idValue);
		if (deleted) { ListenerMessageKit.sendMessage(ACTION_DELETE, this); }
		return deleted;
	}

	@Override
	public boolean update() {
		boolean update = super.update();
		if (update) { ListenerMessageKit.sendMessage(ACTION_UPDATE, this); }
		return update;
	}


	<#list entity.fields as field>
	<#if field.IS_PRIMARY_KEY>
	public ${field.JAVA_TYPE} get${field.ATTR_NAME?cap_first}() {
		Object id = get("${field.NAME}");
		if (id == null)
			return null;
		
		return id instanceof ${field.JAVA_TYPE} ? (${field.JAVA_TYPE})id : new ${field.JAVA_TYPE}(id.toString());
	}
	public void set${field.ATTR_NAME?cap_first}(${field.JAVA_TYPE} ${field.ATTR_NAME?uncap_first}) {
		set("${field.NAME}", ${field.ATTR_NAME?uncap_first});
	}
	@Override
	public boolean equals(Object o) {
		if(o == null){ return false; }
		if(!(o instanceof Base${entity.MODEL_NAME}<?>)){return false;}

		Base${entity.MODEL_NAME}<?> m = (Base${entity.MODEL_NAME}<?>) o;
		if(m.get${field.ATTR_NAME?cap_first}() == null){return false;}

		return m.get${field.ATTR_NAME?cap_first}().compareTo(this.get${field.ATTR_NAME?cap_first}()) == 0;
	}
	<#else>
	/**
	 * ${field.DISPLAY_NAME}
	 * @param ${field.ATTR_NAME?uncap_first}
	 */
	public void set${field.ATTR_NAME?cap_first}(${field.JAVA_TYPE} ${field.ATTR_NAME?uncap_first}) {
		set("${field.NAME}", ${field.ATTR_NAME?uncap_first});
	}
	/**
	 * ${field.DISPLAY_NAME}
	 * @return
	 */
	public ${field.JAVA_TYPE} get${field.ATTR_NAME?cap_first}() {
		return get("${field.NAME}");
	}
	</#if>	
	</#list>


}
